package Practice;
// 交换字符使得字符串相同
public class PracticeSolution1 {
    public static void main(String[] args) {
        System.out.println(minimumSwap("xx","yy"));
    }
    public static int minimumSwap(String s1, String s2) {
        int swapCount = 0;
        if(s1.equals(s2))
            return 0;
        int i = 0;
        boolean flag = true;
        while(i<s2.length()){
            if(s1.charAt(i) == s2.charAt(i)){
                i++;
                continue;
            }
            // 找一个合适的位置进行交换
            int j;
            for(j = i+1 ; j <s2.length() ;j++){
                if(s2.charAt(j) == s2.charAt(i) && s1.charAt(i) == s1.charAt(j)) {
                    char charS1 = s1.charAt(i);
                    char charS2 = s2.charAt(j);
                    s1 = s1.substring(0, i) + charS2 + s1.substring(i + 1, s1.length());
                    s2 = s2.substring(0, j) + charS1 + s2.substring(j + 1, s2.length());
                    swapCount++;
                    i++;
                    flag = true;
                    break;
                }
            }
            if(j == s2.length() && flag){
                // 交换i位置的s1和s2
                flag = false;
                char charS1 = s1.charAt(i);
                char charS2 = s2.charAt(i);
                s1 = s1.substring(0,i)+charS2+s1.substring(i+1,s1.length());
                s2 = s2.substring(0,i)+charS1+s2.substring(i+1,s2.length());
                swapCount++;
            }else if(j == s2.length() && !flag)
                return -1;
        }
        return swapCount;
    }
}
